﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.ComponentModel;

namespace SurveyControl.Controls
{
    [TemplateVisualState(GroupName = NextControl, Name = NextEnable)]
    [TemplateVisualState(GroupName = NextControl, Name = NextDisable)]

    [TemplateVisualState(GroupName = PreviousControl, Name = PreviousEnable)]
    [TemplateVisualState(GroupName = PreviousControl, Name = PreviousDisable)]

    [TemplateVisualState(GroupName = SubmitControl, Name = SubmitEnable)]
    [TemplateVisualState(GroupName = SubmitControl, Name = SubmitDisable)]

    [TemplatePart(Name = NextButton, Type = typeof(FrameworkElement))]
    [TemplatePart(Name = PreviousButton, Type = typeof(FrameworkElement))]
    [TemplatePart(Name = SubmitButton, Type = typeof(FrameworkElement))]

    public class NavigationPart : Control
    {
        #region Constante

        protected const string NextControl = "NextControl";
        protected const string PreviousControl = "PreviousControl";
        protected const string SubmitControl = "SubmitControl";

        protected const string NextEnable = "NextEnable";
        protected const string NextDisable = "NextDisable";

        protected const string PreviousEnable = "PreviousEnable";
        protected const string PreviousDisable = "PreviousDisable";

        protected const string SubmitEnable = "SubmitEnable";
        protected const string SubmitDisable = "SubmitDisable";

        protected const string NextButton = "NextButton";
        protected const string PreviousButton = "PreviousButton";
        protected const string SubmitButton = "SubmitButton";

        #endregion

        protected FrameworkElement nextButton;
        protected FrameworkElement previousButton;
        protected FrameworkElement submitButton;

        #region Public Properties



        public object PreviousContent
        {
            get { return (object)GetValue(PreviousContentProperty); }
            set { SetValue(PreviousContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PreviousContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PreviousContentProperty =
            DependencyProperty.Register("PreviousContent", typeof(object), typeof(NavigationPart), new PropertyMetadata(null));



        public object NextContent
        {
            get { return (object)GetValue(NextContentProperty); }
            set { SetValue(NextContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NextContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NextContentProperty =
            DependencyProperty.Register("NextContent", typeof(object), typeof(NavigationPart), new PropertyMetadata(null));



        public object SubmitContent
        {
            get { return (object)GetValue(SubmitContentProperty); }
            set { SetValue(SubmitContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SubmitContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SubmitContentProperty =
            DependencyProperty.Register("SubmitContent", typeof(object), typeof(NavigationPart), new PropertyMetadata(null));


        #endregion


        public Survey Survey
        {
            get { return (Survey)GetValue(SurveyProperty); }
            set {
                SetValue(SurveyProperty, value);
                UpdateState();
            }
        }

        // Using a DependencyProperty as the backing store for Survey.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SurveyProperty =
            DependencyProperty.Register("Survey", typeof(Survey), typeof(NavigationPart), new PropertyMetadata(null, new PropertyChangedCallback(OnSurveyChanged)));

        

        public NavigationPart()
        {
            this.DefaultStyleKey = typeof(NavigationPart);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            nextButton = GetTemplateChild(NextButton) as FrameworkElement;
            previousButton = GetTemplateChild(PreviousButton) as FrameworkElement;
            submitButton = GetTemplateChild(SubmitButton) as FrameworkElement;

            if (nextButton is ButtonBase)
                (nextButton as ButtonBase).Click += new RoutedEventHandler(nextButton_Click);
            else if (nextButton != null) nextButton.MouseLeftButtonDown += new MouseButtonEventHandler(nextButton_Click);

            if (previousButton is ButtonBase)
                (previousButton as ButtonBase).Click += new RoutedEventHandler(previousButton_Click);
            else if (previousButton != null) previousButton.MouseLeftButtonDown += new MouseButtonEventHandler(previousButton_Click);

            if (submitButton is ButtonBase)
                (submitButton as ButtonBase).Click += new RoutedEventHandler(submitButton_Click);
            else if (submitButton != null) submitButton.MouseLeftButtonDown += new MouseButtonEventHandler(submitButton_Click);

            UpdateState();
        }

        private void onCurrentQuestionChanged(object sender, PropertyChangedEventArgs args)
        {
            if (args.PropertyName != Survey.CurrentQuestionPropertyName)
                return;

            UpdateState();
            
        }

        private void UpdateState ()
        {
            bool result = true;

            if (Survey == null)
            {
                result = VisualStateManager.GoToState(this, PreviousDisable, true);
                result = VisualStateManager.GoToState(this, NextDisable, true);
                result = VisualStateManager.GoToState(this, SubmitDisable, true);
                return;
            }

            if (Survey.HasPrevious)
                result = VisualStateManager.GoToState(this, PreviousEnable, true);
            else
                result = VisualStateManager.GoToState(this, PreviousDisable, true);

            if (Survey.HasNext)
                result = VisualStateManager.GoToState(this, NextEnable, true);
            else
                result = VisualStateManager.GoToState(this, NextDisable, true);

            if (Survey.HasNext)
                result = VisualStateManager.GoToState(this, SubmitDisable, true);
            else
                result = VisualStateManager.GoToState(this, SubmitEnable, true);
        }

        #region Event Handler

        void nextButton_Click(object sender, RoutedEventArgs e)
        {
            if (Survey != null)
                Survey.Next();
        }

        void previousButton_Click(object sender, RoutedEventArgs e)
        {
            if (Survey != null)
                Survey.Previous();
        }

        void submitButton_Click(object sender, RoutedEventArgs e)
        {
            if (Survey != null)
                Survey.Submit();
        }

        private static void OnSurveyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.OldValue is Survey)
                (args.OldValue as Survey).PropertyChanged -= (sender as NavigationPart).onCurrentQuestionChanged;
            if (args.NewValue is Survey)
                (args.NewValue as Survey).PropertyChanged += (sender as NavigationPart).onCurrentQuestionChanged;

            (sender as NavigationPart).UpdateState();
        }

        #endregion
    }
}
